package xcmg.syn.service.order;


import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.syn.dao.mapper.order.MmSwOrderDetailMapper;
import xcmg.syn.dao.mapper.order.MmSwOrderLabelMapper;
import xcmg.syn.dao.mapper.order.MmSwOrderMapper;
import xcmg.syn.service.vo.order.MmSwOrderDetailVO;
import xcmg.syn.service.vo.order.MmSwOrderLabelVO;
import xcmg.syn.service.vo.order.MmSwOrderVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MmSwOrder 后台接口实现类
 */
@Slf4j
@Service
public class MmSwOrderService {

    @Autowired
    private MmSwOrderMapper mmSwOrderMapper;

    @Autowired
    private MmSwOrderDetailMapper mmSwOrderDetailMapper;

    @Autowired
    private MmSwOrderLabelMapper mmSwOrderLabelMapper;

    public List<MmSwOrderVO> queryList(MmSwOrderVO c) {
        List<MmSwOrderVO> list = mmSwOrderMapper.queryList(c);
        if (list == null || list.size() == 0) {
            return list;
        }
        List<Long> ids = list.stream().map(MmSwOrderVO::getOrderId).collect(java.util.stream.Collectors.toList());
        List<MmSwOrderVO> r = collectDetail(ids, list);
        return r;
    }

    public List<MmSwOrderLabelVO> queryLabelList(MmSwOrderLabelVO c) {
        return mmSwOrderLabelMapper.queryList(c);
    }

    private List<MmSwOrderVO> collectDetail(List<Long> ids, List<MmSwOrderVO> list) {
        if (ids == null || ids.size() == 0) {
            return null;
        }
        List<MmSwOrderVO> rr = new ArrayList<>(list.size());
        Map<Long, List<MmSwOrderDetailVO>> r = new HashMap<>(ids.size());
        Map<Long, List<MmSwOrderLabelVO>> lm = new HashMap<>(ids.size());
        List<List<Long>> subIds = Lists.partition(ids, 1000);
        for (List<Long> subId : subIds) {
            doCollectDetail(subId, r);
            doCollectLabels(subId, lm);
        }
        for (MmSwOrderVO m : list) {
            List<MmSwOrderDetailVO> dl = r.get(m.getOrderId());
            m.setMmSwOrderDetailVOList(dl);
            List<MmSwOrderLabelVO> ll = lm.get(m.getOrderId());
            m.setLabelList(ll);
            rr.add(m);
        }
        return rr;
    }

    private void doCollectLabels(List<Long> ids, Map<Long, List<MmSwOrderLabelVO>> lm) {
        MmSwOrderLabelVO v = new MmSwOrderLabelVO();
        v.setOrderIds(ids);
        List<MmSwOrderLabelVO> dl = mmSwOrderLabelMapper.queryList(v);
        if (dl == null || dl.size() == 0) {
            return;
        }
        Map<Long, List<MmSwOrderLabelVO>> map = dl.stream().collect(java.util.stream.Collectors.groupingBy(MmSwOrderLabelVO::getOrderId));
        lm.putAll(map);
    }

    private void doCollectDetail(List<Long> ids, Map<Long, List<MmSwOrderDetailVO>> r) {
        MmSwOrderDetailVO v = new MmSwOrderDetailVO();
        v.setOrderIds(ids);
        List<MmSwOrderDetailVO> dl = mmSwOrderDetailMapper.queryList(v);

        if (dl == null || dl.size() == 0) {
            return;
        }
        Map<Long, List<MmSwOrderDetailVO>> map = dl.stream().collect(java.util.stream.Collectors.groupingBy(MmSwOrderDetailVO::getOrderId));
        r.putAll(map);
    }
}
